package com.webcamtracker.image.color.rgb;

import com.webcamtracker.image.Color;
import com.webcamtracker.utils.MathFunctions;

public class RGBColor implements Color {

    public static final int BLACK_COLOR = 0x00000000;
    private int read;
    private int green;
    private int blue;


    public RGBColor() {
    }


    public RGBColor(int read, int green, int blue) {
        this.read = read;
        this.green = green;
        this.blue = blue;
    }

    public RGBColor(int rgbColor) {
        this();
        this.read = getRead(rgbColor);
        this.green = getGreen(rgbColor);
        this.blue = getBlue(rgbColor);
    }

    public static int getBlue(int rgbColor) {
        return (rgbColor & 0x000000FF);
    }

    public static int getGreen(int rgbColor) {
        return (rgbColor & 0x0000FF00) >> 8;
    }

    public static int getRead(int rgbColor) {
        return (rgbColor & 0x00FF0000) >> 16;
    }


    public int getRed() {
        return read;
    }

    public int getGreen() {
        return green;
    }

    public int getBlue() {
        return blue;
    }

    public void setRead(int read) {
        this.read = read;
    }

    public void setGreen(int green) {
        this.green = green;
    }

    public void setBlue(int blue) {
        this.blue = blue;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RGBColor color = (RGBColor) o;

        if (blue != color.blue) return false;
        if (green != color.green) return false;
        if (read != color.read) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = read;
        result = 31 * result + green;
        result = 31 * result + blue;
        return result;
    }

    public boolean isSimilar(Color searchColor, double threadsHold) {
        double colorMatrix[] = new double[]{this.getRed(), this.getGreen(), this.getBlue()};
        RGBColor rgbColor = ((RGBColor) searchColor);
        double searchColorMatrix[] = new double[]{((RGBColor) searchColor).getRed(), ((RGBColor) searchColor).getGreen(), rgbColor.getBlue()};
        double distance = MathFunctions.euclideanDistance(colorMatrix, searchColorMatrix);
        return distance <= threadsHold;
    }

    public double[] elements() {
        return new double[]{getRed(), getGreen(), getBlue()};
    }

    protected Object clone() throws CloneNotSupportedException {
        super.clone();
        return new RGBColor(getRed(), getGreen(), getBlue());
    }

    public String toString() {
        return "R : " + getRed() +", B : "+ getBlue()+ ", G : "+getGreen();
    }
}
